package com.tiny.work.plane;

import com.google.common.collect.Lists;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 飞行员
 */
public class Pilot implements Serializable {

    /**
     * 累计飞行时间
     */
    private int flightDays;

    private boolean female;

    private Date startTime;

    /**
     * 优先级高到低顺序排列
     */
    private List<IModel> modelList = Lists.newArrayList();

    /**
     * 排期规则
     */
    private List<IRuler> orderRulerList = Lists.newArrayList();

    /**
     * @param journey 单位km
     */
    public PlaneResult fligthResult(BigDecimal journey, BigDecimal weight, ArrangePilot arrangePilot) {
        Context context = new Context(journey, this);
        context.setArrangePilot(arrangePilot);
        context.generateModel();
        return context.getResult(journey, weight);
    }

    public IModel match(Context context) {
        for (IModel iModel : modelList) {
            boolean match = iModel.isMatch(context);
            if (match) {
                return iModel;
            }
        }
        throw new RuntimeException();
    }

    public boolean isInSpecialTime(Date curTime) {
        int i = 0;
        Date date;
        do {
            date = DateUtils.addDay(getStartTime(), 28 * i);
            Date date3 = DateUtils.addDay(getStartTime(), 28 * i + 7);
            if (curTime.compareTo(date) >= 0 && curTime.compareTo(date3) <= 0) {
                return true;
            }
        } while (date.compareTo(DateUtils.toDate("2100-01-01 00:00:00")) < 0 && date.compareTo(curTime) > 0);
        return false;
    }

    public Pilot() {
    }

    public Pilot(List<IModel> modelList) {
        this.modelList = modelList;
    }

    public Pilot(List<IModel> modelList, List<IRuler> orderRulerList) {
        this.modelList = modelList;
        this.orderRulerList = orderRulerList;
    }

    public Pilot(boolean female, Date startTime, List<IModel> modelList) {
        this.female = female;
        this.startTime = startTime;
        this.modelList = modelList;
    }

    public Pilot(boolean female, Date startTime, List<IModel> modelList,
        List<IRuler> orderRulerList) {
        this.female = female;
        this.startTime = startTime;
        this.modelList = modelList;
        this.orderRulerList = orderRulerList;
    }

    public int getFlightDays() {
        return flightDays;
    }

    public Pilot setFlightDays(int flightDays) {
        this.flightDays = flightDays;
        return this;
    }

    public boolean isFemale() {
        return female;
    }

    public Pilot setFemale(boolean female) {
        this.female = female;
        return this;
    }

    public List<IModel> getModelList() {
        return modelList;
    }

    public Pilot setModelList(List<IModel> modelList) {
        this.modelList = modelList;
        return this;
    }

    public Date getStartTime() {
        return startTime;
    }

    public Pilot setStartTime(Date startTime) {
        this.startTime = startTime;
        return this;
    }

    public List<IRuler> getOrderRulerList() {
        return orderRulerList;
    }

    public Pilot setOrderRulerList(List<IRuler> orderRulerList) {
        this.orderRulerList = orderRulerList;
        return this;
    }
}
